﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace 美妆产品管理系统
{
    public class Cartltemsv
    {
        DataClasses1DataContext db = new DataClasses1DataContext();

        public Cart Add(int CustomerID,int GoodID,int Qty)
        {
            Cart cart = null;

            Goods goods = (from g in db.Goods
                           where g.Goods_ID == GoodID
                           select g).First();
            //当前需要添加的Cart对象

            cart = new Cart();
            cart.CustomerID = CustomerID;
            cart.GoodID = goods.Goods_ID;
            cart.GoodName = goods.Goods_Name;
            cart.ListPrice = goods.Unit_Price;
            cart.UnitPrice = goods.Cost_Price.Value;
            cart.Qty = Qty;

            //如果客户当前商品已在购物车，修改商品数量
            int ExistCartItemCount = (from c in db.Cart
                                      where c.CartID == CustomerID && c.GoodID == GoodID
                                      select c).Count();
            if(ExistCartItemCount > 0)
            {
                Cart existCartItem = (from c in db.Cart
                                      where c.CartID == CustomerID && c.GoodID == GoodID
                                      select c).First();
                existCartItem.Qty += Qty;

            }
            else
            {
                db.Cart.InsertOnSubmit(cart);
            }
            db.SubmitChanges();
            return cart;
        }

        public (decimal,decimal) GetTotalPriceByCustomerId(int CustomerID)//计算总价
        {
            List<Cart> list = (from c in db.Cart
                               where c.CustomerID == CustomerID
                               select c).ToList();
            return ((decimal)list.Sum(c => c.ListPrice * c.Qty), (decimal)list.Sum(c => (c.ListPrice - c.UnitPrice) * c.Qty));
        }
    }
}
